package com.example.home.rubbish.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.SearchView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.rubbish.adapter.NewsHotSeanrchAdapter;
import com.example.home.rubbish.adapter.RubbishBannerAdapter;
import com.example.home.rubbish.bean.NewsClassifyBean;
import com.example.home.rubbish.bean.RubbishBannerBean;
import com.example.home.rubbish.bean.SearchNameBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class NewsSearchActivity extends AppCompatActivity {
    private ImageView back;
    private ViewPager vpBanner;
    private SearchView searchView;
    private RecyclerView recyclerViewHotSearch;
    private RecyclerView rcvList;
    private RubbishBannerAdapter rubbishBannerAdapter;
    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_search);
        initView();

        getBanner();
        startAutoScroll();

        getHotData();
    }

    private void getHotData() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-classification/hot/list",
                OKHttpTool.GET,
                NewsClassifyBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsClassifyBean newsClassifyBean = (NewsClassifyBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                               bindHotList(newsClassifyBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindHotList(NewsClassifyBean newsClassifyBean) {
        RecyclerView recyclerView = findViewById(R.id.recyclerViewHotSearch);
        NewsHotSeanrchAdapter newsHotSeanrchAdapter = new NewsHotSeanrchAdapter(NewsSearchActivity.this, newsClassifyBean.getData());

        // 使用GridLayoutManager，每行显示两个条目
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NewsSearchActivity.this, 3);

        recyclerView.setAdapter(newsHotSeanrchAdapter);
        recyclerView.setLayoutManager(gridLayoutManager);
    }




    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/ad-banner/list",
                OKHttpTool.GET,
                RubbishBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RubbishBannerBean rubbishBannerBean = (RubbishBannerBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(rubbishBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindBannershow(RubbishBannerBean rubbishBannerBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        rubbishBannerAdapter = new RubbishBannerAdapter(NewsSearchActivity.this, rubbishBannerBean.getData());
        viewPager.setAdapter(rubbishBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == rubbishBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private void initView() {

        back = findViewById(R.id.back);
        vpBanner = findViewById(R.id.vp_banner);
        searchView = findViewById(R.id.searchView);
        recyclerViewHotSearch = findViewById(R.id.recyclerViewHotSearch);
        rcvList = findViewById(R.id.rcv_list);



        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsSearchActivity.this.finish();
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 在用户提交查询时调用
                performSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void performSearch(String query) {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-classification/list",
                OKHttpTool.GET,
                SearchNameBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        SearchNameBean newsClassifyBean = (SearchNameBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 在数据集合中进行模糊匹配
                                List<SearchNameBean.RowsBean> matchedList = new ArrayList<>();
                                for (SearchNameBean.RowsBean item : newsClassifyBean.getRows()) {
                                    if (item.getName().toLowerCase().contains(query.toLowerCase())) {
                                        matchedList.add(item);
                                    }
                                }

                                // 如果有匹配项，可以选择展示在界面上，这里以直接跳转为例
                                if (!matchedList.isEmpty()) {
                                    // 如果只有一个匹配项，直接跳转
                                    if (matchedList.size() == 1) {
                                        navigateToDetailActivity(matchedList.get(0));
                                    } else {
                                        // 如果有多个匹配项，可以展示在界面上供用户选择
                                        // 你可以使用 RecyclerView 或其他方式展示匹配项列表
                                        // 然后在用户选择后再跳转
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void navigateToDetailActivity(SearchNameBean.RowsBean item) {
        // 根据匹配项跳转到相应的 Activity
        Intent intent = new Intent(NewsSearchActivity.this, SearchDetailActivity.class);
        Gson gson = new Gson();
        intent.putExtra("name", gson.toJson(item));
        // 添加其他需要传递的数据
        startActivity(intent);
    }
}